package com.qf.java2201.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.dto.DoorExcelDto;
import com.qf.java2201.listener.WebDoorListener;
import com.qf.java2201.pojo.Door;
import com.qf.java2201.service.DoorService;
import com.qf.java2201.utils.OssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/door")
//@CrossOrigin(origins = {"http://127.0.0.1:5500"},allowCredentials = "true")
public class DoorController {

    @Autowired
    public DoorService doorService;

    @RequestMapping("/doorPageData")
    // 这里 的  方法内的  @RequestParam  不能省略    省略就无法获取参数了
    public PageVo<Door> getPageData(@RequestParam Map<String,Object> params){
        return doorService.getPageData(params);
    }

    //1. 添加门店
    @RequestMapping("/addDoor")
    public R addDoor(Door door) {
        int result = doorService.addDoor(door);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    //上传照片
    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        String filename = file.getOriginalFilename();  // 得到文件的名字

        InputStream inputStream = file.getInputStream();  // 将file 对象转为 输入流

        // 阿里云存储  返回 上传之后的文件路径
        String path = OssUtil.uploadFile(inputStream,filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;

    }

    // 2.根据id删除门店信息
    @RequestMapping("/deleteDoor")
    public R deleteById(Integer id) {
        int result = doorService.deleteById(id);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("删除成功");
        }else{
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }

    //3.修改门店信息
    @RequestMapping("/updateDoor")
    public R updateDoor(Door door) {
        int result =  doorService.updateDoor(door);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("修改成功");
        }else{
            r.setCode(-1);
            r.setMsg("修改失败");
        }
        return r;
    }

    @RequestMapping("/export")
    public void export(Door door, HttpServletResponse response) throws IOException {
        // 导出学生数据
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("门店信息导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream,Door.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        //PageInfo<Student> pageData = studentService.getPageData(1, new Student());
        List<Door> doors = doorService.selectList(door);

        sheet.doWrite(doors);
    }

    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 下载  模板
        // 设置响应数据的类型
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 读取服务器的模板文件 转为 字节数组
        String path = request.getServletContext().getRealPath("/template/门店信息模板.xlsx");
        File file = new File(path);
        //把文件对象 转为 字节数组对象
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("门店信息导出表", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();
    }

    @Autowired
    private WebDoorListener webDoorListener;

    @RequestMapping("/import")
    public R importExcelData(MultipartFile file) throws IOException {
        // 上传 excel 表格

        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), DoorExcelDto.class, webDoorListener);

        ExcelReaderSheetBuilder sheet = read.sheet();

        sheet.doRead();

        return  R.ok();
    }

}
